package com.dnjn.mes.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.mes.biz.domain.MesAssembly;
import com.dnjn.mes.biz.domain.MesExcept;
import com.dnjn.mes.biz.domain.MesResolvePlan;
import com.dnjn.mes.biz.domain.MesWorkpiece;
import com.dnjn.mes.biz.service.IMesResolvePlanService;
import com.dnjn.mes.biz.service.IMesWorkpieceService;
import com.dnjn.mes.common.core.controller.BaseController;
import com.dnjn.mes.common.core.domain.AjaxResult;
import com.dnjn.mes.common.core.page.TableDataInfo;
import com.dnjn.mes.common.utils.StringUtils;
import com.dnjn.mes.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 异常解决方案表 前端控制器
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
@RestController
@RequestMapping("/biz/mesResolvePlan")
public class MesResolvePlanController extends BaseController {

    @Autowired
    protected IMesResolvePlanService baseService;
    @Autowired
    protected IMesWorkpieceService workpieceService;

    @PostMapping("/export")
    public void export(HttpServletResponse response, MesResolvePlan domain) {
        List<MesResolvePlan> list = baseService.selectAllPlanList(domain);
        //List<MesResolvePlan> list = baseService.list(Wrappers.query(domain));
        ExcelUtil<MesResolvePlan> util = new ExcelUtil<MesResolvePlan>(MesResolvePlan.class);
        util.exportExcel(response, list, "参数数据");
    }

    @PostMapping("importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MesResolvePlan> util = new ExcelUtil<MesResolvePlan>(MesResolvePlan.class);
        List<MesResolvePlan> list = util.importExcel(file.getInputStream());
        String message = baseService.importData(list, updateSupport);
        return success(message);
    }

    /**
     * 新增解决方案
     */
    @Operation(summary = "新增解决方案")
    /*@Parameters({
            @Parameter(name = "description", description = "故障解决方案"),
            @Parameter(name = "workpieceId", description = "工件id")
    })*/
    @PostMapping("/savePaln")
    public AjaxResult savePaln(@Validated @RequestBody MesResolvePlan domain, @RequestHeader("equipmentId") Long equipmentId) {
        domain.setCreateBy(getUsername());
        domain.setEquipmentId(equipmentId);
        //存入关联异常id表
        return toAjax(baseService.savePlan(domain));
    }


    @Operation(summary = "获取方案列表根据零部件id")
    @PostMapping("/getPlanPageByWorkpieceId/{workpieceId}")
    public AjaxResult getPlanPageByWorkpieceId(@PathVariable Long workpieceId, @RequestHeader("equipmentId") Long equipmentId) {
        Assert.notNull(workpieceId, "workpieceId不能为空！");
        //List<MesResolvePlan> list = baseService.lambdaQuery().eq(MesResolvePlan::getWorkpieceId, workpieceId).list();
        List<MesResolvePlan> list = baseService.getPlanPageByWorkpieceId(workpieceId, equipmentId);
        return success(list);
    }

    @Operation(summary = "获取方案列表分页根据模块id")
    @PostMapping("/getPlansByAssemblyId")
    public AjaxResult getPlansByAssemblyId(@RequestParam(required = false) @Parameter(description = "workpieceName") String searchKey, @RequestParam Long assemblyId, @RequestParam(defaultValue = "1") Integer current,
                                           @RequestParam(defaultValue = "10") Integer size, @RequestHeader("equipmentId") Long equipmentId) {
        Assert.notNull(assemblyId, "assemblyId不能为空！");
        PageHelper.startPage(current, size);
        List<MesResolvePlan> list = baseService.getPlansByAssemblyId(assemblyId, searchKey, equipmentId);
        PageInfo pageInfo = new PageInfo(list);
        return success(pageInfo);
    }

    @Operation(summary = "获取方案列表")
    @PostMapping("/getPlanList")
    public AjaxResult getPlanList(@RequestHeader("equipmentId") Long equipmentId) {
        QueryWrapper<MesResolvePlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", "0");
        queryWrapper.eq("equipment_id", equipmentId);
        List<MesResolvePlan> list = baseService.list(queryWrapper);
        for (MesResolvePlan mesResolvePlan : list) {
            Long workpieceId = mesResolvePlan.getWorkpieceId();
            mesResolvePlan.setWorkpieceName(workpieceService.getById(workpieceId).getWorkpieceName());
        }
        return success(list);
    }


    /**
     * 获取方案,根据id获取
     */
    @Operation(summary = "获取方案")
    @PostMapping("/getPlanById/{planId}")
    public AjaxResult getPlanById(@PathVariable Long planId) {
        Assert.notNull(planId, "planId不能为空！");
        MesResolvePlan mesResolvePlan = baseService.getPlan(planId);
        return success(mesResolvePlan);
    }


    /**
     * 修改解决方案
     */
    @Operation(summary = "修改解决方案")
    /*@Parameters({
            @Parameter(name = "planId", description = "planId"),
            @Parameter(name = "description", description = "故障解决方案"),
            @Parameter(name = "workpieceId", description = "工件id")
    })*/
    @PutMapping("/editPlan")
    public AjaxResult editPlan(@Validated @RequestBody MesResolvePlan domain) {
        Assert.notNull(domain.getPlanId(), "planId不能为空！");
        return toAjax(baseService.updateById(domain));
    }

    /**
     * 删除方案
     */
    @Operation(summary = "删除方案")
    @DeleteMapping("/deledtPlanById/{planId}")
    public AjaxResult deledtPlanById(@PathVariable("planId") Long planId) {
        Assert.notNull(planId, "planId不能为空！");
        Long count = baseService.removePlanById(planId);
        if (count == 1) {
            return success("删除成功");
        }else {
            return error("删除失败");
        }
    }
}